package net.minecraft.world.spawner;

import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityData;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.mob.PhantomEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.stat.ServerStatHandler;
import net.minecraft.stat.Stats;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.GameRules;
import net.minecraft.world.LocalDifficulty;
import net.minecraft.world.SpawnHelper;

public class PhantomSpawner implements SpecialSpawner {
	private int cooldown;

	@Override
	public void spawn(ServerWorld world, boolean spawnMonsters, boolean spawnAnimals) {
		if (spawnMonsters) {
			if (world.getGameRules().getBoolean(GameRules.DO_INSOMNIA)) {
				Random random = world.random;
				this.cooldown--;
				if (this.cooldown <= 0) {
					this.cooldown = this.cooldown + (60 + random.nextInt(60)) * 20;
					if (world.getAmbientDarkness() >= 5 || !world.getDimension().hasSkyLight()) {
						for (ServerPlayerEntity serverPlayerEntity : world.getPlayers()) {
							if (!serverPlayerEntity.isSpectator()) {
								BlockPos blockPos = serverPlayerEntity.getBlockPos();
								if (!world.getDimension().hasSkyLight() || blockPos.getY() >= world.getSeaLevel() && world.isSkyVisible(blockPos)) {
									LocalDifficulty localDifficulty = world.getLocalDifficulty(blockPos);
									if (localDifficulty.isHarderThan(random.nextFloat() * 3.0F)) {
										ServerStatHandler serverStatHandler = serverPlayerEntity.getStatHandler();
										int i = MathHelper.clamp(serverStatHandler.getStat(Stats.CUSTOM.getOrCreateStat(Stats.TIME_SINCE_REST)), 1, Integer.MAX_VALUE);
										int j = 24000;
										if (random.nextInt(i) >= 72000) {
											BlockPos blockPos2 = blockPos.up(20 + random.nextInt(15)).east(-10 + random.nextInt(21)).south(-10 + random.nextInt(21));
											BlockState blockState = world.getBlockState(blockPos2);
											FluidState fluidState = world.getFluidState(blockPos2);
											if (SpawnHelper.isClearForSpawn(world, blockPos2, blockState, fluidState, EntityType.PHANTOM)) {
												EntityData entityData = null;
												int k = 1 + random.nextInt(localDifficulty.getGlobalDifficulty().getId() + 1);

												for (int l = 0; l < k; l++) {
													PhantomEntity phantomEntity = EntityType.PHANTOM.create(world, SpawnReason.NATURAL);
													if (phantomEntity != null) {
														phantomEntity.refreshPositionAndAngles(blockPos2, 0.0F, 0.0F);
														entityData = phantomEntity.initialize(world, localDifficulty, SpawnReason.NATURAL, entityData);
														world.spawnEntityAndPassengers(phantomEntity);
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
